package com.note.simpleadmin.module.shop.service;

import com.note.simpleadmin.entity.TbBrand;
import com.note.simpleadmin.entity.TbColumn;
import com.note.simpleadmin.enu.DeleteEnum;
import com.note.simpleadmin.exception.DataNotExistException;
import com.note.simpleadmin.module.shop.dao.BrandRepository;
import com.note.simpleadmin.module.shop.dao.ColumnRepository;
import com.note.simpleadmin.utils.IdGenerator;
import com.note.simpleadmin.vo.PageBean;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.util.ArrayList;
import java.util.List;

/***
 *@ClaseName:BrandService
 *
 *@Decription:
 *
 *@Author: xubingbing
 *
 *@Date:2019/1/31 0031 20:59
 */
@Service
public class ColumnService {
    @Autowired
    private ColumnRepository columnRepository;
    public TbColumn getOne(String id) {
        return columnRepository.findByIdAndIsDelete(id, DeleteEnum.NORMAL.getValue());
    }
    public void delete(String id) throws DataNotExistException
    {
        TbColumn tbColumn=getOne(id);
        if(tbColumn==null)
        {
            throw new DataNotExistException("数据不存在");
        }
        columnRepository.delete(tbColumn);
    }
    public void update(TbColumn param)throws DataNotExistException
    {
        TbColumn tbColumn=getOne(param.getId());
        if(tbColumn==null)
        {
            throw new DataNotExistException("数据不存在");
        }
        columnRepository.save(param);
    }
    public void save(TbColumn param)
    {
        param.setId(IdGenerator.id());
        param.setIsDelete(DeleteEnum.NORMAL.getValue());
        columnRepository.save(param);
    }
    public List<TbColumn> findAll()
    {
        return columnRepository.findAll();
    }
    public PageBean findByPage(String name,Integer pageNumber)
    {
        if (pageNumber == null) {
            pageNumber = 0;
        }
        else
        {
            pageNumber=pageNumber-1;
        }
        Sort sort = new Sort(Sort.Direction.DESC, "sort");
        PageRequest pageRequest = new PageRequest(pageNumber, 20, sort);
        Specification<TbColumn> specification=new Specification<TbColumn>() {
            private static final long serialVersionUID = 1L;
            @Override
            public Predicate toPredicate(Root<TbColumn> root, CriteriaQuery<?> query, CriteriaBuilder criteriaBuilder) {
                List<Predicate> predicates = new ArrayList<>();
                if (StringUtils.isNotBlank(name)) {
                    predicates.add(criteriaBuilder.equal(root.get("name"), name));
                }
                return  criteriaBuilder.and(predicates.toArray(new Predicate[predicates.size()]));
            }
        };
        Page<TbColumn> page = columnRepository.findAll(specification,pageRequest);
        List<TbColumn> environments=page.getContent();
        PageBean<TbColumn> pageBean=new PageBean(page.getTotalPages(),page.getTotalElements(),page.isFirst(),page.isEmpty(),page.getNumber(), environments);;
        return pageBean;
    }

}
